// pages/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlService: app.urlService,
    modalShow: false,
    tipurl: "",
    meData: {},
    orderData: {},
    keyword: '',
    tipTxt: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      modalShow: false
    })
    app.token = wx.getStorageSync('token');
    if (app.token == "") {
      wx.navigateTo({
        url: "../login/index"
      })
      return;
    }
    wx.showLoading();
    var that = this;
    wx.request({
      url: this.data.urlService + '/me',
      method: "GET",
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (data) {
        if (data.statusCode == 401) {
          wx.showToast({
            title: '登录超时，请重新登录',
            icon: "error"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
          return;
        }
        var json = JSON.parse(data.data);
        if (json.success) {
          that.setData({
            meData: json.data
          })

          wx.request({
            url: that.data.urlService + '/api/v1/order/getOrder',
            method: "GET",
            dataType: "JSON",
            header: {
              'content-type': 'application/json',
              'Authorization': "Bearer " + app.token
            },
            success: function (data) {
              if (data.statusCode == 401) {
                wx.showToast({
                  title: '登录超时，请重新登录',
                  icon: "error"
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: "../login/index"
                  })
                }, 1000);
                return;
              }
              wx.hideLoading()
              var json = JSON.parse(data.data);
              if (json.success) {
                that.setData({
                  orderData: json.data
                })
              } else {
                wx.showToast({
                  title: json.msg,
                  icon: 'error'
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: json.msg,
            icon: 'error'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  returnGood: function () {
    this.setData({
      modalShow: true,
      tipurl: "returnOrderList",
      tipTxt: "不输入可查询所有客户"
    })
  },

  hireGood: function () {
    this.setData({
      modalShow: true,
      tipurl: "addLease",
      tipTxt: "请输入客户名称"
    })
  },

  closeModal: function () {
    this.setData({
      modalShow: false
    })
  },

  loginout: function () {
    wx.showModal({
      title: '确认',
      content: '确定要退出吗？',
      success(res) {
        if (res.confirm) {
          app.token = '';
          wx.setStorageSync('token', '')
          wx.showToast({
            title: '退出成功',
            icon: "succese"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
        }
      }
    })
  },

  searchCustome: function () {
    var url = this.data.tipurl;
    if (url == 'addLease' && (this.data.keyword == null || this.data.keyword == '')) {
      wx.showToast({
        title: '请输入客户信息',
        icon: "error"
      })
      return;
    }
    var that = this;
    wx.navigateTo({
      url: "../" + url + "/index?keyword=" + that.data.keyword
    })
  },

  toOrderList: function () {
    wx.navigateTo({
      url: "../returnOrderList/index"
    })
  },

  inputKeyword: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  }
})